package com.dex4d.utils;

import android.text.TextUtils;

import com.dex4d.constants.AppConstants;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Currency;
import java.util.Locale;

/**
 * 作者    LiQingyi
 * 时间    2018/11/14 11:08
 * 文件    dex4d-android
 * 描述
 */
public class MoneyUtils {

    private static Currency getCurrency() {
        return Currency.getInstance(getLocal());
    }

    private static Locale getLocal() {
        String code = SPUtil.getString(AppConstants.SP_CURRENCY_KEY, "usd");
        if (TextUtils.equals(code, "usd")) {
            return Locale.US;
        } else if (TextUtils.equals(code, "cny")) {
            return Locale.CHINA;
        } else {
            return Locale.US;
        }
    }

    public static String getSymbol() {
        String code = SPUtil.getString(AppConstants.SP_CURRENCY_KEY, "usd");
        if (TextUtils.equals(code, "usd")) {
            return "$";
        } else if (TextUtils.equals(code, "cny")) {
            return "¥";
        } else {
            return "$";
        }
    }


    public static String getCurrencyCode() {
        return getCurrency().getCurrencyCode();
    }

    public static String showMoney(String money) {
        if (TextUtils.isEmpty(money)) {
            money = "0";
        }
        BigDecimal bigDecimal = new BigDecimal(money);
        bigDecimal.setScale(2, BigDecimal.ROUND_DOWN);
        DecimalFormat format = new DecimalFormat("0.00");
        return format.format(bigDecimal.doubleValue());
    }

}
